package com.yupi.yupicturebackend.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.yupicturebackend.common.BaseResponse;
import com.yupi.yupicturebackend.common.DeleteRequest;
import com.yupi.yupicturebackend.common.ResultUtils;
import com.yupi.yupicturebackend.common.UserHolder;
import com.yupi.yupicturebackend.exception.BusinessException;
import com.yupi.yupicturebackend.exception.ErrorCode;
import com.yupi.yupicturebackend.model.dto.file.UploadPictureResult;
import com.yupi.yupicturebackend.model.dto.picture.*;
import com.yupi.yupicturebackend.model.entity.Picture;
import com.yupi.yupicturebackend.model.entity.User;
import com.yupi.yupicturebackend.model.enums.PictureStatusEnum;
import com.yupi.yupicturebackend.model.enums.UserRoleEnum;
import com.yupi.yupicturebackend.model.vo.PictureVO;
import com.yupi.yupicturebackend.model.vo.UserVO;
import com.yupi.yupicturebackend.service.PictureService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@RequestMapping("/Picture")
public class PictureController {
    @Resource
    private PictureService pictureService;

    @ApiOperation("删除照片")
    @PostMapping("/delete")
    // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse deletePicture(@RequestBody DeleteRequest deleteRequest) {
        //judge is exist
        if (deleteRequest == null || deleteRequest.getId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //only amdin user
        UserVO user = UserHolder.getUser();
        if(user == null || user.getUserRole().equals(UserRoleEnum.ADMIN.getValue())){
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }

        //operate the database
        pictureService.removeById(deleteRequest.getId());
        return ResultUtils.success("ok");
    }

    /**
     * 仅管理员可以用
     * @param
     * @return
     */
    @ApiOperation("更新照片")
    @PostMapping("/update")
    // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse updatePicture(@RequestBody UploadPictureResult uploadPictureResult) {
        //实体类和dto类转换
        Picture picture = new Picture();
        BeanUtil.copyProperties(uploadPictureResult,picture);

        //data validate

        //judge is exist
        if (picture == null || picture.getId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //operate the database
        pictureService.saveOrUpdate(picture);
        return ResultUtils.success("ok");
    }

    /**
     * 仅管理员可用
     * @param id
     * @return
     */
    @ApiOperation("根据id获取图片")
    @PostMapping("/get/{id}")
    // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse getById(@PathVariable Long id) {
        //data validate
    //query database
        //return encapsulated class
        return ResultUtils.success("ok");
    }

    /**
     * 用户根据id获取脱敏图片
     * @param id
     * @return
     */
    @ApiOperation("根据id获取图片用户可用")
    @GetMapping("/get/vo/{id}")
    public BaseResponse getByIdVO(@PathVariable Long id) {
        //data validate
        // select database
        // conversion between entity calss and encapsulate class
        return ResultUtils.success("ok");
    }

    /**
     * admin use
     * @param
     * @return
     */
    @GetMapping("/list/page")
    // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("分页获取图片")
    public BaseResponse getPicturePage(@RequestBody PictureQueryRequest queryRequest) {
        int current = queryRequest.getCurrent();
        int pageSize = queryRequest.getPageSize();
        QueryWrapper<Picture> queryWrapper = pictureService.Query_Wrapper(queryRequest);

        //query database

        // return result
        return ResultUtils.success("ok");
    }

    @ApiOperation("分页获取图片封装类")
    @GetMapping("/list/page/vo")
    public BaseResponse getPictureVOPage(@RequestBody PictureQueryRequest queryRequest) {
    //restrict web crawles

        //user only query picture of passed review
        queryRequest.setReviewStatus(PictureStatusEnum.PASS.getValue());
        //get encapsulate class
        QueryWrapper<Picture> queryWrapper = pictureService.Query_Wrapper(queryRequest);
        return ResultUtils.success();
    }

    @ApiOperation("编辑图片")
    @PostMapping("/edit")
    // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse PictureEdit(@RequestBody PictureEditRequest editRequest) {
        //conversion between entity calss and encapsulate class
        Picture picture = new Picture();
        // list-> string
        //edit time
        //data validate
        //judge is exist
        //judge admin
        //operate database
        return ResultUtils.success();
    }
    @ApiOperation("通用标签")
    @PostMapping("/common")
    // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse CommonTags() {
        CommonTags commonTags = new CommonTags();
        commonTags.setTags(Arrays.asList("java","表情包"));
        commonTags.setCategorys(Arrays.asList("高清","风景"));
        return ResultUtils.success(commonTags);
    }
    @ApiOperation("通用标签")
    @PostMapping("/common")
    // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse PictureReview(@RequestBody PictureReviewRequest reviewRequest) {
      if (reviewRequest == null || reviewRequest.getId() <= 0){
          throw new BusinessException(ErrorCode.PARAMS_ERROR);
      }
      pictureService.PictureReview(reviewRequest);
      return ResultUtils.success();
    }

}

